#include <iostream>
using namespace std;
//递归算法解决八皇后问题。总共有92种解法。
int c[20], n = 8, cnt = 0;
void print() 
{
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (j == c[i]) cout << "# ";
			else cout << ". ";
		}
		cout << endl;
	}
	cout << endl;
}
void search(int r)
{
	if (r == n)
	{
		print();
		++cnt;
		return;
	}
	for (int i = 0; i < n; ++i)
	{
		c[r] = i;
		int ok = 1;
		if (r != 0)
		{
			for (int a = 1; a <= r; a++)
			{
				if (c[r - a] == i || c[r - a] == i - a || c[r - a] == i + a)
					ok = 0;
			}
		}
		if (ok) search(r + 1);
	}

}
int main()
{
	search(0);
	cout << "八皇后问题共有: " << cnt << "种解法!" << endl;
}